############################ 
####Codes	for Imputation###
############################ 

rm(list=ls())
library(Amelia)
library(foreign)
require(Amelia)

setwd("/Users/yuehou/Dropbox/KingReplication/Ongoing Work")
data1= read.csv("Rueda_Working_Copy16_trimmed_1970.csv")
set.seed(1000)
summary(data1)

##################
# Set Bounds

bds <- matrix(c(13, 0, 1), nrow = 1, ncol = 3)
bds<- rbind(c(51,0,1),bds)
bds<- rbind(c(19,0,100),bds)
bds<- rbind(c(29,0,10),bds)
bds<- rbind(c(21,0,100),bds)
bds<- rbind(c(60,0,100),bds)
bds<- rbind(c(55,0,100),bds)
bds<- rbind(c(61,0,100),bds)
bds<- rbind(c(23,0,1),bds)
bds



#Set Priors
pr<-matrix(NA,15,5)
pr[,5]<-0.85   # set CI=0.85

# final_ia5010

for (i in 1:6){
   pr[i,1]<-i+48  
   pr[i,2]<-45
   pr[i,3]<-1.8
   pr[i,4]<-2.0
}

#minwage_jf 

for (i in 7:9){
pr[i,1]<-i+7
pr[i,2]<-49
pr[i,3]<-0.4
pr[i,4]<-0.7
}



# govem_yh2  
 
 for (i in 10:12){
     pr[i,1]<-i-9
     pr[i,2]<-72
     pr[i,3]<-21
     pr[i,4]<-25
}

for (i in 13:15){
    pr[i,1]<-173+i
    pr[i,2]<-72
    pr[i,3]<-10
    pr[i,4]<-16
}


amelia.out9<-amelia(data1,m=8,ts="year",cs="countrid",intrcs=TRUE,ords=c("vk_corp"),polytime=3,idvars=c("country","nd70","nd80","nd90","nd00","dccggx","generos","ldc","ldc_imports","open","finop","femla","unemp","hkcorp","wagecor","gdpgr","govpart","elect1","elmon","elday","leftc","lefts","leftv","rightc","rights","rightv","tcdemc","tcdems","tcdemgs","rightgs","ia5010","min","minwagnational","govem","oecd_priv_total","deca70","deca80","deca85","deca90","arm_emp_civ","arm_civ_labf","dGermany","oecd_serviceemp","oecd_totalemp","oecd_unemp","priv","arm_pop"),bounds=bds,
lags=c("hkcorp_1","govem_yh2","final_ia5010","arm_generos","arm_finop","arm_open","oecd_femla","minwag_jf","ldc_imports_scaled"),leads=c("hkcorp_1","govem_yh2","final_ia5010","arm_generos","arm_finop","arm_open","vk_corp","oecd_femla","minwag_jf","ldc_imports_scaled"),priors=pr,autopri=0.05) 

amelia.out1 <- amelia.out9

par(mfrow = c(4, 4),cex=0.4)
compare.density(output=amelia.out1,var="final_ia5010")
compare.density(output=amelia.out1,var="govem_yh")
compare.density(output=amelia.out1,var="govem_yh2")
compare.density(output=amelia.out1,var="vk_corp")
compare.density(output=amelia.out1,var="minwag_jf")
compare.density(output=amelia.out1,var="arm_generos")
compare.density(output=amelia.out1,var="hkcorp_1")
compare.density(output=amelia.out1,var="oecd_debt")
compare.density(output=amelia.out1,var="ldc_imports_scaled")
compare.density(output=amelia.out1,var="oecd_femla")
compare.density(output=amelia.out1,var="oecd_gdpgr")
compare.density(output=amelia.out1,var="arm_finop")
compare.density(output=amelia.out1,var="arm_open")
compare.density(output=amelia.out1,var="oecd_priv")
compare.density(output=amelia.out1,var="oecdilo_govem")



par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "minwag_jf", cs = i)
}

par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "final_ia5010", cs = i)
}

par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "arm_generos", cs = i)
}

par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "govem_yh2", cs = i)
}

par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "govtotalem_ilo", cs = i)
}

par(mfrow = c(5, 4),cex=0.4)
for(i in unique(data1$countrid)){
	tscsPlot(amelia.out1, var = "oecd_debt", cs = i)
}

#Save each file as .dta  under the working directory (STATA format)
save(amelia.out9, file = "imputations17.RData")
write.amelia(obj=amelia.out9,file.stem="imputed_data_v17_",format="dta")


#############################
## Graphs for Appendix B
#############################

par(mfrow = c(4, 4),cex=0.4)
compare.density(output=amelia.out1,var="final_ia5010")
compare.density(output=amelia.out1,var="govem_yh")
compare.density(output=amelia.out1,var="govem_yh2")
compare.density(output=amelia.out1,var="vk_corp")
compare.density(output=amelia.out1,var="minwag_jf")
compare.density(output=amelia.out1,var="arm_generos")
compare.density(output=amelia.out1,var="hkcorp_1")
compare.density(output=amelia.out1,var="oecd_debt")
compare.density(output=amelia.out1,var="ldc_imports_scaled")
compare.density(output=amelia.out1,var="oecd_femla")
compare.density(output=amelia.out1,var="oecd_gdpgr")
compare.density(output=amelia.out1,var="arm_finop")
compare.density(output=amelia.out1,var="arm_open")
compare.density(output=amelia.out1,var="oecd_priv")
compare.density(output=amelia.out1,var="oecdilo_govem")



